/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmti.skeleton.web.controller;

import com.cmti.commons.util.DataGridModel;
import com.cmti.skeleton.dto.SysLogDto;
import com.cmti.skeleton.entity.SysLog;
import com.cmti.skeleton.service.SysLogService;
import com.github.pagehelper.Page;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 系统日志Controller
 *
 * @author danny
 */
@Controller
@RequestMapping("/syslog")
public class SysLogController {

    private final Log log = LogFactory.getLog(getClass());

    @Autowired
    private SysLogService service;

    @RequestMapping("/ui")
    public String listUI() {
        return "sys/sysLog";
    }

    /**
     * 使用layui时使用该方法
     *
     * @param dto
     * @param model
     * @param dgm
     * @param limit 每页的条数
     */
    @RequestMapping("/dataLayui")
    public void listDataLayui(SysLogDto dto, ModelMap model, DataGridModel dgm, @RequestParam(value = "limit", defaultValue = "10") int limit) {
        dgm.setRows(limit);
        dto.setDgm(dgm);
        Page<SysLog> list = (Page<SysLog>) service.getList(dto);
//        layui需要返回以下信息
//        数据状态的字段名称，默认：code 成功的状态码，默认：0 
//        状态信息的字段名称，默认：msg 
//        数据总数的字段名称，默认：count 
//        数据列表的字段名称，默认：data
        model.addAttribute("code", 0).addAttribute("msg", "").addAttribute("data", list.getResult()).addAttribute("count", list.getTotal());
    }

    /**
     * 使用easyui时使用该方法
     *
     * @param dto
     * @param model
     * @param dgm
     */
    @RequestMapping("/data")
    public void listData(SysLogDto dto, ModelMap model, DataGridModel dgm) {
        dto.setDgm(dgm);
        Page<SysLog> list = (Page<SysLog>) service.getList(dto);
        model.addAttribute("rows", list.getResult()).addAttribute("total", list.getTotal());
    }
}
